package com.ypan.uninstaller;

import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.viewpagerindicator.TitlePageIndicator;

/**
 * @ClassName: MainActivity
 * @Description: 主Activity
 * @author Administrator
 * @date 2012-8-7 下午10:25:09
 */
public class MainActivity extends SherlockFragmentActivity implements
		OnPageChangeListener, OnClickListener {

	private static final String TAG = "Uninstall";
	TitlePageIndicator mTitleIndicator;
	ViewPager mViewPager;
	View mBottonBar;
	TextView mAboutTV;
	TextView mUninstallTV;
	TextView mSelAllTV;
	UninstallFragmentAdapter mFragAdapter;

	// private static int THEME1 = R.style.Theme_Sherlock;
	// private static int THEME2 = R.style.Sherlock.Light.DarkActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// setTheme(THEME2);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.main_tab);
		MobclickAgent.onError(this);
		initView(savedInstanceState);
		MainApp.sMainActivity = this;
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		MainApp.sMainActivity = this;
		MobclickAgent.onResume(this);
		super.onResume();
	}

	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	private void initView(Bundle savedInstanceState) {

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mFragAdapter = new UninstallFragmentAdapter(this,
				getSupportFragmentManager());
		mViewPager.setAdapter(mFragAdapter);
		mTitleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
		mTitleIndicator.setViewPager(mViewPager);
		mTitleIndicator.setOnPageChangeListener(this);
		mBottonBar = (View) findViewById(R.id.bottom_bar);
		mAboutTV = (TextView) findViewById(R.id.settings_tv);
		mAboutTV.setOnClickListener(this);
		mUninstallTV = (TextView) findViewById(R.id.uninstall_tv);
		mUninstallTV.setOnClickListener(this);
		mSelAllTV = (TextView) findViewById(R.id.select_all_tv);
		mSelAllTV.setOnClickListener(this);
		// This is a workaround for http://b.android.com/15340 from
		// http://stackoverflow.com/a/5852198/132047
		// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
		BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
				R.drawable.bg_striped);
		bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		getSupportActionBar().setBackgroundDrawable(bg);

		BitmapDrawable bgSplit = (BitmapDrawable) getResources().getDrawable(
				R.drawable.bg_striped_split_img);
		bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
		// }
	}

	@Override
	public void onPageScrollStateChanged(int pos) {

	}

	@Override
	public void onPageScrolled(int pos, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int pos) {
		if (mFragAdapter.getTitleID(pos) == R.string.tab_backup) {
			 mAboutTV.setText(R.string.delete);
			 mUninstallTV.setText(R.string.install);
			 mSelAllTV.setText(R.string.select_all);
		} else {
			 mAboutTV.setText(R.string.pref_settings);
			 mUninstallTV.setText(R.string.uninstall);
			 mSelAllTV.setText(R.string.select_all);
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		int pos = mViewPager.getCurrentItem();
		BaseFragment f=(BaseFragment)mFragAdapter.getItem(pos);
		switch (id) {
		case R.id.settings_tv:
			f.onDeleteClick();
			break;
		case R.id.uninstall_tv:{
			f.onUninstallClick();
		}
			break;
		case R.id.select_all_tv:{
			if(mSelAllTV.getText() == getResources().getString(R.string.unselect_all)){
				f.onUnSelAllClick();
				mSelAllTV.setText(R.string.select_all);
			}else{
				f.onSelAllClick();
				mSelAllTV.setText(R.string.unselect_all);
			}
		}
			break;
		default:
			break;
		}
	}

}
